﻿using Common;
using enterprise.Website.Core.Filter;
using enterprise.Website.Core.Models.Manage;
using FreeSql.Context;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;


namespace enterprise.Website.Core.ApiControllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AdminSqlController : ControllerBase
    {
        private readonly SqlContext _sqlContext;
        public AdminSqlController(SqlContext sqlContext)
        {
            _sqlContext = sqlContext;
        }

        /// <summary>
        /// 执行查询Sql
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        [LoginAuthorize]
        public async Task<ApiResult<AdminSqlLayuiModel>> Post([FromBody] AdminSqlModel data)
        {
            var result = new ApiResult<AdminSqlLayuiModel> { Success = false, Message = "执行失败" };
            if (data != null&&!data.ExecuteSql.IsNullOrWhiteSpace())
            {
                try
                {
                    var resData = await _sqlContext.ExecuteDataTableAsync(data.ExecuteSql);
                    if (resData != null)
                    {
                        List<LayuiColsModel> cols = new List<LayuiColsModel>();
                        foreach (DataColumn column in resData.Columns)
                        {
                            cols.Add(new LayuiColsModel
                            {
                                field = column.ColumnName.InitialsLower(),
                                align = "center",
                                title = column.ColumnName,
                                style = "height:30px"
                            });
                        }
                        result.Data = new AdminSqlLayuiModel { table = resData, cols = cols };
                        result.Message = "执行成功";
                        result.Success = true;
                    }
                }
                catch (Exception ex)
                {
                    result.Message = ex.Message;
                    result.Success = false;
                }
            }
            return result;
        }

        /// <summary>
        /// 执行非查询Sql
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPut]
        [LoginAuthorize]
        public async Task<ApiResult> Put([FromBody] AdminSqlModel data)
        {
            var result = new ApiResult { Success = false, Message = "执行失败" };
            if (data != null && !data.ExecuteSql.IsNullOrWhiteSpace())
            {
                try
                {
                    var resData = await _sqlContext.ExecuteNonQueryAsync(data.ExecuteSql);
                    result.Message = $"执行成功,受影响的行数:[{resData}]";
                    result.Success = true;
                }
                catch (Exception ex)
                {
                    result.Message = ex.Message;
                    result.Success = false;
                }
            }
            return result;
        }
    }
}
